﻿using Dapper;
using System;
using BCEntity.Trading.Purchase.GoodsPurchasePaidRecord;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Trading.Purchase.GoodsPurchasePaidRecord
{
    /// <summary>
    /// 商品采购支付记录
    /// </summary>
    public class GoodsPurchasePaidRecordData : IGoodsPurchasePaidRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM goodspurchasepaidrecord";
        private const string QUERY = @"SELECT GoodsPurchasePaidRecordId,CreateAdminId,CreateAdminName,GoodsPurchaseId,GoodsPurchaseChecklistId,PaidGoodsInventoryCount,PaidAmount,PaidType,PaidTime,PaidDate,PaidCredential FROM goodspurchasepaidrecord";
        public GoodsPurchasePaidRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查商品采购支付记录是否存在
        public async Task<bool> Exists(long goodsPurchasePaidRecordId)
        {
            string sql = string.Format(@"{0} WHERE GoodsPurchasePaidRecordId=?GoodsPurchasePaidRecordId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchasePaidRecordId", goodsPurchasePaidRecordId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加商品采购支付记录
        public async Task<GoodsPurchasePaidRecordEntity> Add(GoodsPurchasePaidRecordEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("goodspurchasepaidrecord", new string[] { "GoodsPurchaseId", "GoodsPurchaseChecklistId", "CreateAdminId", "CreateAdminName",
                "PaidGoodsInventoryCount", "PaidAmount", "PaidType", "PaidTime", "PaidDate","PaidCredential" });
            sql = string.Format("{0} {1} WHERE GoodsPurchasePaidRecordId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?GoodsPurchaseId", entity.GoodsPurchaseId, DbType.Int64, size: 20);
            parameters.Add("?GoodsPurchaseChecklistId", entity.GoodsPurchaseChecklistId, DbType.Int64, size: 20);
            parameters.Add("?PaidGoodsInventoryCount", entity.PaidGoodsInventoryCount, DbType.Int32, size: 11);
            parameters.Add("?PaidAmount", entity.PaidAmount, DbType.Decimal, size: 16);
            parameters.Add("?PaidType", entity.PaidType.ToString(), DbType.AnsiString);
            parameters.Add("?PaidTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?PaidDate", entity.PaidDate, DbType.DateTime);
            parameters.Add("?PaidCredential", entity.PaidCredential?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<GoodsPurchasePaidRecordEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改商品采购支付记录
        public async Task<GoodsPurchasePaidRecordEntity> Update(GoodsPurchasePaidRecordEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("goodspurchasepaidrecord", new string[] { "CreateAdminId", "CreateAdminName", "GoodsPurchaseId", "GoodsPurchaseChecklistId",
                "PaidGoodsInventoryCount", "PaidAmount", "PaidType", "PaidDate", "PaidCredential"}, new string[] { "GoodsPurchasePaidRecordId" });
            sql = string.Format("{0} {1} WHERE GoodsPurchasePaidRecordId=?GoodsPurchasePaidRecordId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchasePaidRecordId", entity.GoodsPurchasePaidRecordId, DbType.Int64, size: 20);
            parameters.Add("?GoodsPurchaseId", entity.GoodsPurchaseId, DbType.Int64, size: 20);
            parameters.Add("?GoodsPurchaseChecklistId", entity.GoodsPurchaseChecklistId, DbType.Int64, size: 20);
            parameters.Add("?PaidGoodsInventoryCount", entity.PaidGoodsInventoryCount, DbType.Int32, size: 11);
            parameters.Add("?PaidAmount", entity.PaidAmount, DbType.Decimal, size: 16);
            parameters.Add("?PaidType", entity.PaidType.ToString(), DbType.AnsiString);
            parameters.Add("?PaidDate", entity.PaidDate, DbType.DateTime);
            parameters.Add("?PaidCredential", entity.PaidCredential?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<GoodsPurchasePaidRecordEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除商品采购支付记录
        public async Task<bool> Delete(long goodsPurchasePaidRecordId)
        {
            string sql = @"DELETE FROM goodspurchasepaidrecord WHERE GoodsPurchasePaidRecordId=?GoodsPurchasePaidRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchasePaidRecordId", goodsPurchasePaidRecordId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取商品采购支付记录
        public async Task<GoodsPurchasePaidRecordEntity> GetEntity(long goodsPurchasePaidRecordId)
        {
            string sql = string.Format(@"{0} WHERE GoodsPurchasePaidRecordId=?GoodsPurchasePaidRecordId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchasePaidRecordId", goodsPurchasePaidRecordId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<GoodsPurchasePaidRecordEntity>(sql, parameters);
        }
        #endregion

        #region 获取商品采购支付记录列表
        public async Task<IEnumerable<GoodsPurchasePaidRecordEntity>> GetList(long goodsPurchaseId)
        {
            string sql = string.Format(@"{0} WHERE GoodsPurchaseId=?GoodsPurchaseId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?GoodsPurchaseId", goodsPurchaseId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<GoodsPurchasePaidRecordEntity>(sql, parameters);
        }
        #endregion

        #region 获取商品采购支付记录分页
        public async Task<(IEnumerable<GoodsPurchasePaidRecordEntity>, int)> Query(long goodsPurchaseId, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("GoodsPurchaseId=?GoodsPurchaseId");
            parameters.Add("?GoodsPurchaseId", goodsPurchaseId, DbType.Int64);

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("PaidTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("PaidTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("PaidTime>=?Start AND PaidTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM goodspurchasepaidrecord {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "PaidTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<GoodsPurchasePaidRecordEntity, int>(sql, parameters);
        }
        #endregion
    }
}



